import { Scene, GameObjects, Cameras, type Types } from "phaser";
import backgroundImg from "../images/background.png";
import overlayImg from "../images/overlay.png";
import gameoverImg from "../images/gameover.png";
import titleImg from "../images/title.png";
import spritesImg from "../images/sprites.png";
import spritesJson from "../json/sprites.json?url";
import snowGlsl from "../glsl/snow.glsl?url";
import musicAudio from "../audio/music.mp3";
import throwAudio from "../audio/throw.mp3";
import moveAudio from "../audio/move.mp3";
import hitSnowmanAudio from "../audio/hit-snowman.mp3";
import gameoverAudio from "../audio/gameover.mp3";

// 相机对象
let camera: Cameras.Scene2D.Camera;

// 场景元素
let loadText: GameObjects.Text;

export class Preloader extends Scene {
    constructor() {
        super("Preloader");
    }
    init() {
        camera = this.cameras.main;
    }
    preload() {
        // 加载中
        loadText = this.add
            .text(camera.width / 2, camera.height / 2, "Loading...", {
                fontFamily: "Arial",
                fontSize: 74,
                color: "#e3f2ed",
                stroke: "#203c5b",
                strokeThickness: 6,
                shadow: {
                    offsetX: 2,
                    offsetY: 2,
                    color: "#2d2d2d",
                    blur: 4,
                    stroke: true,
                    fill: false,
                },
            })
            .setOrigin(0.5);
        // 加载图片
        this.load.image("background", backgroundImg);
        this.load.image("overlay", overlayImg);
        this.load.image("gameover", gameoverImg);
        this.load.image("title", titleImg);
        // 加载雪碧图
        this.load.atlas("sprites", spritesImg, spritesJson);
        // 加载glsl
        this.load.glsl("snow", snowGlsl);
        // 加载音频
        this.load.audio("music", musicAudio);
        this.load.audio("throw", throwAudio);
        this.load.audio("move", moveAudio);
        this.load.audio("hit-snowman", hitSnowmanAudio);
        this.load.audio("gameover", gameoverAudio);
    }
    create() {
        // 最高分全局存储
        this.registry.set("highScore", 0);
        // 创建动画
        this.createAnims();

        // 如果不能自动播放声音，则添加点击事件
        if (this.sound.locked) {
            loadText.setText("Click to Start");
            this.input.once("pointerdown", () => {
                this.scene.start("Menu");
            });
        } else {
            this.scene.start("Menu");
        }
    }
    // 创建动画
    createAnims() {
        this.anims.create({
            key: "die",
            frames: this.anims.generateFrameNames("sprites", { prefix: "die", start: 0, end: 0, zeroPad: 3 }),
        });

        this.anims.create({
            key: "idle",
            frames: this.anims.generateFrameNames("sprites", { prefix: "idle", start: 0, end: 3, zeroPad: 3 }),
            yoyo: true,
            frameRate: 8,
            repeat: -1,
        });

        this.anims.create({
            key: "throwStart",
            frames: this.anims.generateFrameNames("sprites", { prefix: "throw", start: 0, end: 8, zeroPad: 3 }),
            frameRate: 26,
        });

        this.anims.create({
            key: "throwEnd",
            frames: this.anims.generateFrameNames("sprites", { prefix: "throw", start: 9, end: 11, zeroPad: 3 }),
            frameRate: 26,
        });

        this.anims.create({
            key: "snowmanIdleBig",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-big-idle", start: 0, end: 3 }),
            yoyo: true,
            frameRate: 8,
            repeat: -1,
        });

        this.anims.create({
            key: "snowmanWalkBig",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-big-walk", start: 0, end: 7 }),
            frameRate: 8,
            repeat: -1,
        });

        this.anims.create({
            key: "snowmanThrowStartBig",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-big-throw", start: 0, end: 5 }),
            frameRate: 20,
        });

        this.anims.create({
            key: "snowmanThrowEndBig",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-big-throw", start: 6, end: 8 }),
            frameRate: 20,
        });

        this.anims.create({
            key: "snowmanDieBig",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-big-die", start: 0, end: 4 }),
            frameRate: 14,
        });

        this.anims.create({
            key: "snowmanIdleSmall",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-small-idle", start: 0, end: 3 }),
            yoyo: true,
            frameRate: 8,
            repeat: -1,
        });

        this.anims.create({
            key: "snowmanWalkSmall",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-small-walk", start: 0, end: 7 }),
            frameRate: 8,
            repeat: -1,
        });

        this.anims.create({
            key: "snowmanThrowStartSmall",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-small-throw", start: 0, end: 5 }),
            frameRate: 20,
        });

        this.anims.create({
            key: "snowmanThrowEndSmall",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-small-throw", start: 6, end: 8 }),
            frameRate: 20,
        });

        this.anims.create({
            key: "snowmanDieSmall",
            frames: this.anims.generateFrameNames("sprites", { prefix: "snowman-small-die", start: 0, end: 4 }),
            frameRate: 14,
        });
    }
}
